package com.iotechn.unimall.admin.api.supplier;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.SupplierDO;
import com.iotechn.unimall.data.mapper.SupplierMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class AdminSupplierServiceImpl implements AdminSupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public Page<SupplierDO> list(String supplierName,String supplierContacts,String supplierPhone,Integer page, Integer limit, Long adminId, String status) throws ServiceException {
        Wrapper<SupplierDO> wrapper = new EntityWrapper<SupplierDO>();
        if (!StringUtils.isEmpty(supplierName)) {
         wrapper.like("supplier_name", supplierName);
        }
        if (!StringUtils.isEmpty(supplierContacts)) {
            wrapper.like("supplier_contacts", supplierContacts);
        }
        if (!StringUtils.isEmpty(supplierPhone)) {
            wrapper.like("supplier_phone", supplierPhone);
        }

        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        wrapper.orderBy("id", false);
        Integer count = supplierMapper.selectCount(wrapper);
        List<SupplierDO> SupplierDOS = supplierMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(SupplierDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SupplierDO create(SupplierDO supplierDO, Long adminId) throws ServiceException {
        supplierDO.setAdminId(adminId);
        Date now = new Date();
        supplierDO.setGmtCreate(now);
        supplierDO.setGmtUpdate(now);
        supplierDO.setStatus(1);//创建时状态为正常
        supplierDO.setSupplierCategoryId(0);//分类暂时设置为0，后期添加
        if (supplierMapper.insert(supplierDO) > 0) {
            return supplierDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(SupplierDO supplierDO, Long adminId) throws ServiceException {
        Long id = supplierDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        supplierDO.setGmtUpdate(now);
        if (supplierMapper.updateById(supplierDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (supplierMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    public SupplierDO detail(Long id, Long adminId) throws ServiceException {
        return supplierMapper.selectById(id);
    }

    @Override
    public String updateStatus(Long id, Integer status, Long adminId) throws ServiceException {
        SupplierDO supplierDO = supplierMapper.selectById(id);
        if (supplierDO == null) {
            return "fail";
        }
        supplierDO.setStatus(status);//冻结
        Date now = new Date();
        supplierDO.setGmtUpdate(now);
        supplierDO.setAdminId(adminId);
        if (supplierMapper.updateById(supplierDO) > 0) {//删除改为假删除，更改状态为0
            return "ok";
        }
        return "fail";
    }

    @Override
    public List<SupplierDO> listSuppliersByPara(String status) throws ServiceException {
        List<SupplierDO> supplierDOList=supplierMapper.selectList(new EntityWrapper<SupplierDO>().eq(!StringUtils.isEmpty(status),"status",status));
        return supplierDOList;
    }


}